<?php
/**
 * 朱志浩 2020年7月14日17:47:36 订单相关
 * getOrder() => 解析提交的订单
 */
namespace app\index\model;
use app\common\controller\Base as B;
use app\common\controller\Payment;
use app\common\lib\Redis;
use app\common\model\Account;
use app\common\model\Amount;
use app\common\model\Logs;
use app\common\model\Rec;
use think\Db;
use think\Model;

class Order extends Model
{
    protected $name = 'order';
    public function getOrder($user)
    {
        try {
            $all = Redis::Instance()->hGet('order', $user['code']);
            $all = json_decode($all, true);
            if (empty($all)) {
                return ['code' => -1, 'msg' => '提交失败，请重新提交'];
            }
            switch ($all['category1']) {
                case 1:
                    $price = 'unit_price';
                    break;
                // case 3:
                //     $price = 'seckill_price';
                //     break;
                default:
                    $price = 'unit_price';

            }
            $list = [];

            $other['cash_amount']     = 0;
            $other['consume_integral_amount'] = 0;
            $other['total']          = 0;
            // $quotation = Ztpay::quotation('usdt');
            // $quotation = $quotation?$quotation:['cny'=>1];
            foreach ($all as $k => $v) {
                $info = Goods::get($v['id']);
                if (empty($info)) {
                    continue;
                }
                $other['total'] += $info[$price] * $v['num'];
                $other['cash_amount'] += $info[$price] * $v['num'];
                // if($info['category1']==2){
                //     $other['cash_amount']=0;
                //     $other['cash_amount'] += ($info[$price] * $v['num'])-$info[$price] * $v['num']*config($user['level_id'].'_ded');
                //     $other['consume_integral_amount'] += $info[$price] * $v['num'];
                // }

                //计算USDT价格
                // $usdt_amount = round($info[$price] / $quotation['cny'], 6);

                // $other['usdt_total'] += $usdt_amount * $v['num'];
                $info['price'] = $info[$price];
                $info['num']   = $v['num'];
                $list[$k]      = $info;
            }

            // $address = Address::get_default($user['id'], $user['code']);
            Redis::Instance()->hSet('order_pay', $user['code'], json_encode([
                'goods'      => $list, //下单的商品数据
                'other'      => $other, //下单的价格
                'category1'  => $all['category1'], //商品类别，1：批购商品 2：其他商品
            ]));
            // dump(json_decode(Redis::Instance()->hGet('order_pay', $user['code'],true) ));die;
            return ['code' => 1, 'result' => $list, 'total' => $all['category1'] == 2 ? '现金:' . $other['cash_amount'] . '积分:' . $other['consume_integral_amount'] : $other['total']];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public function getPayInfo($code)
    {
        try {
            $info = Redis::Instance()->hGet('order_pay', $code);
            return json_decode($info, true);
        } catch (\Exception $e) {
            return false;
        }
    }

    public function payment($param, $user)
    {
        Db::startTrans();
        try {
            if (!request()->isPost()) {
                return json(['code' => -5, 'msg' => '请求拒绝']);
            }
            if (md5($param['pay_password']) != $user['pay_password']) {
                return json(['code' => -1, 'msg' => '支付密码错误']);
             }
            //开团测试直接找问题
            $group_model = new Group();
            // $group_model->open_group();
            // $group_model->join_group();
            // die;
            //获取缓存订单信息
            $pay_info = $this->getPayInfo($user['code']);
            if (!$pay_info) {
                return json(['code' => -1, 'msg' => '加载失败！']);
            }
            
            if ($param['address_id'] <= 0) {
                return json(['code' => -1, 'msg' => '请选择收货地址']);

                // return $this->jsonError('请选择收货地址');
            }
            $address = db::name('address')->where(['id'=>$param['address_id']])->find();
            $time = time();
            switch ($param['pay_way']) {
                case "balance": //电子钱包支付
                case "bonus": //电子钱包支付
                case "vip_bonus": //会员佣金支付
                case "consume_integral": //商城积分支付
                   
                    //单个支付
                    $pay_name = ['coins' => '电子钱包', 'bonus' => '奖金', 'balance' => '余额', 'consume_integral' => '积分', 'usdt' => 'USDT', 'group_bonus' => '抢购券', 'dis_ticket' => '折扣券'];
                    $cash     = $pay_info['other']['total'];
                    if(in_array($param['pay_way'],['consume_integral'])){//组合支付
                        if ($user['consume_integral'] < $pay_info['other']['consume_integral_amount']) {
                            return json(['code' => -1, 'msg' => '积分不足']);
                        }
                        if ($user['balance'] < $cash) {
                            return json(['code' => -1, 'msg' => $pay_name[$param['pay_way']].'奖金不足']);
                        }
                    }else{
                        if ($user[$param['pay_way']] < $cash) {
                            return json(['code' => -1, 'msg' => $pay_name[$param['pay_way']] . '不足']);
                        }
                    }
                    if ($pay_info['category1'] == 1) {
                            //产生记录
                            $res = $group_model->generate_group($user, $pay_info['goods'][0], $param['pay_way'], $param['address_id'], $cash,$param['remark'],$address);
                            if ($res['code'] != 1) {
                                return json(['code' => -2, 'msg' => $res['msg']]);
                            }
                            //发起支付
                            $result = $group_model->join_group($res['id'], $user, $param['pay_way'],$pay_info['goods'][0]);
                            
                    }
                    if ($result['code'] == 1) {
                        Db::commit();
                        if($result['user']['is_auto']==1 && $result['is_open']==1){
                             //自动复购
                            $group_model->auto_buy($result['user'],$result['order'],$result['goods']);
                        }
                        return json(['code' => $result['code'], 'msg' => $result['msg']]);
                    } else {
                        Db::rollback();
                        return json(['code' => -1, 'msg' => $result['msg'] ? $result['msg'] : '提交失败']);
                    }
                    break;
                case "wechat":
                case "alipay":
                    $cash     = $pay_info['other']['cash_amount'];
                    //查询金币是否充足
                    if ($user['gold'] < config('group_cost')) {
                        return json(['code' => -1, 'msg' => '金币不足']);
                    }
                    //线上支付
                    $return_url = 'http://' . $_SERVER['SERVER_NAME'] . '/my_order';
                    if ($pay_info['category1'] ==1) {
                        $pay_way    = $param['pay_way'];
                        $title      = '购买三军帅酒';
                        // $return_url = 'http://' . $_SERVER['SERVER_NAME'] . '/index/group/info.html';
                        if ($param['pay_way'] == 'wechat') {
                            if(!is_weixin()){
                                return json(['code' => -1, 'msg' => '请在公众号内使用']);
                            }
                            $code       = 2;
                            $notify_url = 'http://' . $_SERVER['SERVER_NAME'] . '/96e79218965eb72c92a549dd5a330114';
                            file_put_contents('notify.txt', "下单\r\n", FILE_APPEND);
                        } else {
                            $code       = 3;
                            $notify_url = 'http://' . $_SERVER['SERVER_NAME'] . '/e3ceb5881a0a1fdaad01296d7554868d';
                        }
                        //产生记录
                        $res = $group_model->generate_group($user, $pay_info['goods'][0], $param['pay_way'], $param['address_id'], $cash,$param['remark'],$address);
                        if ($res['code'] != 1) {
                            return json(['code' => -1, 'msg' => $res['msg']]);
                        }
                    } else {
                        $title = '购买三军帅酒';
                        if ($param['pay_way'] == 'wechat') {
                           
                            $pay_way    = 'wechat';
                            $code       = 4;
                            $notify_url = 'http://' . $_SERVER['SERVER_NAME'] . '/85e02efd3dbb8ecb8852006b520d92bd';
                        } else {
                            $code       = 3;
                            $pay_way    = 'alipay';
                            $notify_url = 'http://' . $_SERVER['SERVER_NAME'] . '/6fcea009cfc469bcd448324f7b06e210';
                        }
                        $res = $this->generate($pay_info, $param['pay_way'], $user, $time, $cash, $pay_info['other']['cash_amount'],$pay_info['other']['consume_integral_amount'],$param['remark']);
                    }
                    
                    $result = $this->pull_pay($pay_way, $res['sn'], 'mp', $cash, $title, $notify_url, $return_url);
                    file_put_contents('notify/notify.txt', "支付参数\r\n",var_export($result,true), FILE_APPEND);

                    Db::commit();
                    return json(['code' => $code, 'result' => $result]);
                    break;
                default:
                    return json(['code' => -1, 'msg' => '请选择支付方式']);
            }
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function other_pay($order_id, $user, $time, $pay_way,$g_id='',$num='')
    {
        try {
            $order = parent::get($order_id);
            $msg   = '购买商品，订单ID【' . $order_id . '】';
            if (in_array($pay_way, ['consume_integral', 'agent_integral'])) {
                $one = $pay_way == 'consume_integral' ? 'bonus' : 'agent_bonus';
                Account::create_Inc($user['id'], $order['cash_amount'], $one, '购买商品', $order_id, '-');
                Account::create_Inc($user['id'], $order['consume_integral_amount'], $pay_way, '购买商品', $order_id, '-');
            } else {
                // if ($pay_way == 'usdt') {
                //     $total_amount = $order['total_amount'] / config('usdt_rate');
                // } else {
                //     $total_amount = $order['total_amount'];
                // }
                Account::create_Inc($user['id'], $order['total_amount'], $pay_way, '购买商品', $order_id, '-');
            }
            Logs::add($user['id'], $user['nickname'], $msg);
            parent::save(['pay_state' => 2, 'pay_time' => $time, 'state' => 2], ['id' => $order_id]);
            $cart_list = Redis::Instance()->get('cart_pay_' . $user['code']);
            if ($cart_list) {
                $cart = new Cart();
                foreach (json_decode($cart_list, true) as $v) {
                    $cart->where('cart_id', $v)->delete();
                }
            }
            db('product')->where(['id'=>$g_id])->setInc('sales',$num);
            //查询团加业绩
            if($order['order_type']==1){
                Db::name('order_group_parent')->where(['create_time'=>['<=',$time],'state'=>1,'product_id'=>$g_id])->order('id desc')->setInc('new_bonus',$order['total_amount']);
                if($user['level_id']==1){
                    User::update(['level_id' => 2,'is_active'=>1], ['id' => $user['id']]);
                    db('member')->where(['id'=>$user['RefereeId']])->setInc('rec_ps');
                }
            }
            //各种奇葩的骚操作
            /*  $res = $this->processing($order, $user, $time);
            if ($res != true) {
            Db::rollback();
            return ['code' => -1, 'msg' => '下单失败' . $res];
            }*/
         
            // 积分区和零售区发奖
            if ($order['order_type'] !== 3) {
                $member_level=db('member_level')->select();
                $rec= new Rec();
                $res= $rec->shop_award($user['id'], $order['cash_amount'],$member_level,$order,$user);
                if ($res != true) {
                    return $res;
                }
                $is_capping_time=$time+180*86400;
                User::update(['is_capping_time'=>$is_capping_time], ['id' => $user['id']]);

            }
            /*折扣区发奖*/
            // if($order['order_type']==4){
            //     $rec = new Rec();
            //     $product=Db::name()
            //     $res = $rec->zhekou_award($user['id'], $order['cash_amount'],$user);
            //     if ($res != true) {
            //         return $res;
            //     }
            // }
            Redis::Instance()->hDel('order', $user['code']);
            Redis::Instance()->hDel('order_pay', $user['code']);
            Redis::Instance()->set('cart_pay_' . $user['code'], null);

            return ['code' => 1, 'msg' => '支付成功'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage() . $e];
        }
    }

    public function clear_redis($code)
    {
        $cart_list = Redis::Instance()->get('cart_pay_' . $code);

        if ($cart_list) {
            $cart = new Cart();
            foreach (json_decode($cart_list, true) as $v) {
                $cart->where('cart_id', $v)->delete();
            }
        }
        Redis::Instance()->hDel('order', $code);
        Redis::Instance()->hDel('order_pay', $code);
        Redis::Instance()->set('cart_pay_' . $code, null);
    }

    public function processing($order, $user, $time)
    {

        try {
            $rec = new Rec();
            switch ($order['order_type']) {
                case 1:
                    //代理专区的订单
                    //更改代理等级
                    $agent = db('level_agent')->where('id', $order['agent_id'])->find();
                    User::update([
                        'agent_id'             => $order['agent_id'],
                        'is_active'            => 2,
                        'agent_money'          => $agent['amount'],
                        'agent_capping'        => $agent['capping'],
                        'agent_capping_amount' => $agent['amount'] * $agent['capping'],
                    ], ['id' => $user['id']]);
                    Rec::update(['status' => $order['agent_id']], ['new_member_id' => $user['id']]);
                    //各种奖
                    $res = $rec->award($user['id'], $order['total_amount']);
                    if ($res != true) {
                        return $res;
                    }
                    //累计业绩
                    Amount::add($user['id'], $order['id'], $order['total_amount'], 1);
                    //判断是否升级
                    $res = $rec->upgrade($user['id']);
                    if ($res != true) {
                        return $res;
                    }

                    break;
                case 3:

                    break;
                case 4:

                    break;
                default:
                    //其他专区相关操作
                    //如果不是组合支付才有一系列的操作
                    if (in_array($order['pay_way'], ['vip_integral', 'agent_integral'])) {
                        return true;
                    }
                    //累计业绩
                    Amount::add($user['id'], $order['id'], $order['total_amount'], 2);
                    //各种奖金
                    $res = $rec->award_other($user['id'], $order['total_amount']);
                    if ($res != true) {
                        return $res;
                    }
                    //检测上级是否有升级
                    $res = $rec->upgrade_partner($user['id']);
                    if ($res != true) {
                        return $res;
                    }
                    //如果下单会员是普通会员检测是否可以升级为正式会员
                    if ($user['is_active'] < 1) {
                        $sum = $this->where('uid = ' . $user['id'] . ' and state > 1 and order_type > 3')->sum('total_amount');
                        if ($sum >= config('is_vip')) {
                            User::update(['is_active' => 1], ['id' => $user['id']]);
                            Rec::update(['status' => 1], ['new_member_id' => $user['id']]);
                        }
                    }
                    //判断消费奖
                    if ($user['is_day_other'] < 1) {
                        $res = (new Amount())->consumption($user['id'], $order['id']);
                        if ($res != true) {
                            return $res;
                        }
                    }

            }

            //商家奖励
            $res = $this->business($order['id']);
            if ($res !== true) {
                return $res;
            }

            return true;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    public function business($order_id)
    {
        try {
            $op = db('order_product')->where('order_id', $order_id)->select();
            foreach ($op as $k => $v) {
                if ($v['store_id']) {
                    $money = ($v['price'] * $v['count']) * $v['store_tp'];
                    if ($money && $money > 0) {
                        $msg  = '促销分润';
                        $msg2 = $msg . ':商品ID【' . $v['product_id'] . '】';
                        $res  = Account::create_Inc($v['store_id'], $money, 'vip_bonus', $msg, $v['product_id'], '+', $msg2, 11);
                        if ($res != 1) {
                            return $res;
                        }
                        $user = User::get($v['store_id']);
                        Logs::add($user['id'], $user['code'], $msg2);
                    }
                }
            }

            return true;
        } catch (\Exception $e) {
            return $e->getMessage();
        }

    }

    /**
     * @param $driver -- 驱动方式，wechat=>微信支付 alipay => 支付宝支付
     * @param $sn -- 订单号
     * @param $gateway -- 支付方式，wap => h5支付
     * @param $total -- 金额
     * @param $title -- 标题
     * @param $notify_url -- 回调地址
     * @param $return_url -- 同步跳转地址
     * @return bool|mixed|string
     */
    public function pull_pay($driver, $sn, $gateway, $total, $title, $notify_url, $return_url)
    {
        $options = [
            'out_trade_no' => $sn,
            'total_fee'    => $total,
            'body'         => $title,
            'notify_url'   => $notify_url,
            'gateway'      => $gateway,
            'return_url'   => $return_url,
        ];
        $Payment = new Payment($options);
        $result  = $Payment->driver($driver)->payment();

        return $result;
    }

    /**
     * @param $pay_info -- 下单的商品
     * @param $pay_way -- 支付方式
     * @param $user -- 下单用户
     * @param $time -- 下单时间
     * @param $cash -- 下单的现金金额
     * @param $integral -- 下单的积分金额
     * @return array|bool
     */
    public function generate($pay_info, $pay_way, $user, $time, $total, $cash_amount, $consume_integral = 0,$remark='')
    {
        
        try {
            $sn = date('YmdHis') . generate(7) . $user['id'];
            $id = parent::insertGetId([
                'sn'                      => $sn,
                'uid'                     => $user['id'],
                'code'                    => $user['code'],
                'nickname'                => $user['nickname'],
                'pay_way'                 => $pay_way,
                'create_time'             => $time,
                'total_amount'            => $total,
                'cash_amount'             => $cash_amount==0?$total:$cash_amount,
                'consume_integral_amount' => $consume_integral,
                'address_id'              => $pay_info['address_id'],
                'order_type'              => $pay_info['category1'],
                'category2'               => $pay_info['category2'],
                'remark'=>$remark
            ]);
            $orders = [];
            foreach ($pay_info['goods'] as $k => $v) {
                $orders[$k]['order_id']               = $id;
                $orders[$k]['product_id']             = $v['id'];
                $orders[$k]['title']                  = $v['title'];
                $orders[$k]['count']                  = $v['num'];
                $orders[$k]['image']                  = $v['image'];
                $orders[$k]['price']                  = $v['price'];
                $orders[$k]['amount']                 = $v['price'] * $v['num'];
                $orders[$k]['cash_price']             = $v['price'] * $v['num']*config($user['level_id'].'_ded');
                $orders[$k]['integral_price']         = $v['ticket_price'];
                $orders[$k]['consume_integral_price'] = $v['price'];
                $orders[$k]['store_id']               = $v['store_id'];
                // $store                                = User::get($v['store_id']);
                // if ($store) {
                //     $orders[$k]['store_tp'] = $store['store_tp'];
                // }

            }
            Db::name('order_product')->insertAll($orders);
            
            return ['id' => $id, 'sn' => $sn];
        } catch (\Exception $e) {

            return false;
        }

    }

    /**
     * 原点 2020年8月13日10:19:30 拼团成功的下单
     *
     * @param $sn
     * @param $time
     * @return bool
     */
    public static function generate_group($group, $time,$goods)
    {
        try {
            
            $id    = parent::insertGetId([
                'sn'           => $group['sn'],
                'uid'          => $group['uid'],
                'code'         => $group['code'],
                'nickname'     => $group['nickname'],
                'pay_way'      => $group['pay_way'],
                'create_time'  => $time,
                'total_amount' => $group['amount'],
                'cash_amount'  => $group['amount'],
                'address_id'   => $group['address_id'],
                'name'              => $group['name'],
                'phone'              => $group['phone'],
                'address'              => $group['address'],
                'category1'   => 1,
                'pay_state'    => 2,
                'pay_time'     => $time,
                'group_state'     => 1,
                'state'        => 2,
                // 'is_group'     => 1,
                'remark' => $group['remark']
                
            ]);
            db('order_goods')->insert([
                'order_id'       => $id,
                'goods_id'     => $goods['id'],
                'title'          => $goods['title'],
                'num'          => $group['num'],
                'price'          => $goods['unit_price'],
                'cash_price'     => $group['amount'],
                'integral_price' => 0,
                'img'          => $goods['img'],
                'amount'         => $group['amount'] * $group['num'],
            ]);
            return true;
        } catch (\Exception $e) {
            echo $e;
            return false;
        }
    }

    /**
     * * 原点 2020年8月20日16:34:48 抽奖下单
     *
     * @param $product_id -- 中奖商品ID
     * @param $user -- 中奖用户信息
     * @return array
     */
    public function generate_luck($product_id, $user)
    {
        try {
            $time    = time();
            $goods   = Goods::get($product_id);
            $address = Address::get_default($user['id'], $user['code']);
            $id      = parent::insertGetId([
                'sn'              => date('YmdHis') . generate(7) . $user['id'],
                'uid'             => $user['id'],
                'code'            => $user['code'],
                'nickname'        => $user['nickname'],
                'pay_way'         => 'luck_integral',
                'create_time'     => $time,
                'total_amount'    => get_price($goods),
                'cash_amount'     => 0,
                'integral_amount' => 0,
                'address_id'      => $address['id'],
                'order_type'      => 4,
                'pay_state'       => 2,
                'pay_time'        => $time,
                'state'           => 2,
            ]);
            db('order_product')->insert([
                'order_id'       => $id,
                'product_id'     => $goods['id'],
                'title'          => $goods['title'],
                'count'          => 1,
                'price'          => get_price($goods),
                'cash_price'     => $goods['cash_price'],
                'integral_price' => $goods['integral_price'],
                'image'          => $goods['image'],
                'amount'         => get_price($goods),
            ]);
            return ['code' => 1, 'result' => $id];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public function get_orders($param, $uid, $state, $where = [])
    {
        try {
            $where = array_merge($where, ['uid' => $uid, 'is_del' => 0]);
            if ($param['state']) {
                $where['state'] = $param['state'];
            } else {
                $where['state'] = ['neq', 9];
            }
            $list = parent::all(function ($query) use ($param, $where) {
                $query->where($where)->field('id,sn,total_amount,state,group_state,cancel_time,order_type,cash_amount,consume_integral_amount')->page($param['page'], 10)
                    ->order('create_time desc');
            });

            $time = '1603036800';
            foreach ($list as &$v) {

                $v['goods'] = Db::name('order_product')->where('order_id', $v['id'])->select();
                $v['state'] = $state[$v['state']];
                if ($v['create_time'] > $time) {
                    $v['is_haowu'] = 1;
                } else {
                    $v['is_haowu'] = 0;
                }
                if ($v['cancel_time']) {
                    $v['cancel_time'] = date('Y-m-d H:i:s', $v['cancel_time']);
                }

            }

            return json(['code' => 1, 'result' => $list, 'page' => $param['page']]);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function edit($param)
    {
        try {
            $order = parent::get($param['id']);
            if (empty($order)) {
                return json(['code' => -1, 'msg' => '订单不存在']);
            }
            $time = time();
            switch ($param['type']) {
                case 1:
                    //提醒发货
                    if ($order['is_remind'] == 1) {
                        return json(['code' => -1, 'msg' => '您已提醒发货了']);
                    }
                    $save = ['is_remind' => 1, 'remind' => $time];
                    $msg  = '已提醒发货';
                    break;
                case 2:
                    //确认收货
                    if ($order['state'] != 3) {
                        return json(['code' => -1, 'msg' => '此订单不可确认收货']);
                    }
                    $save = ['state' => 4, 'receipt_time' => $time];
                    $msg  = '已确认收货';
                    if ($order['order_type'] == 2) {
                        $rec       = new Rec();
                        $area      = Db::name('member_address')->where('id=' . $order['address_id'])->field('prov_id,city_id,county_id')->find();
                        $order_pro = db('order_product')->where('order_id=' . $order['id'])->field('product_id,count')->find();
                        $product   = db('product')->find($order_pro['product_id']);
                        
                        $res       = $rec->shop_agent($product, $area, $order_pro['count']);
                        if ($res != true) {
                            return $res;
                        }
                    }
                    break;
                case 3:
                    //取消订单
                    if ($order['state'] != 1) {
                        return json(['code' => -1, 'msg' => '此订单不可取消']);
                    }
                    $save = ['state' => 9, 'cancel_time' => $time];
                    $msg  = '已取消';
                    break;
                default:
                    return json(['code' => -1, 'msg' => '操作失败']);
            }
            parent::save($save, ['id' => $param['id']]);
            return json(['code' => 1, 'msg' => $msg]);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function get_info($id)
    {
        try {

            $list = parent::where('o.id', $id)
                ->alias('o')
                ->join('member_address a', 'o.address_id = a.id', 'LEFT')
                ->join('express e', 'o.express_id = e.ID', 'left')
                ->join('area a1', 'a.prov_id = a1.area_id', 'left')
                ->join('area a2', 'a.city_id = a2.area_id', 'left')
                ->join('area a3', 'a.county_id = a3.area_id', 'left')
                ->field('o.*,e.Name,a.prov_id,a.city_id,a.county_id,a.details,a.name,a.phone,a1.area_name as prov_name,a2.area_name as city_name,a3.area_name as county_name')
                ->find();
            foreach ($list as &$v) {
                $v['goods'] = Db::name('order_product')->where('order_id', $v['id'])->select();
            }

            return $list;

        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}
